<script setup lang="ts">
import { RouterLink, RouterView, useRouter } from 'vue-router'
import { onMounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { User, SwitchButton, ArrowDown, Star } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import NotificationComponent from '@/components/NotificationComponent.vue'

const router = useRouter()
const userStore = useUserStore()

const userMenuVisible = ref(false)

onMounted(async () => {
  const isLoggedIn = await userStore.checkLoginStatus()
  if (!isLoggedIn) {
    router.push('/login')
  }
})

const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })

    const result = await userStore.userLogout()
    ElMessage.success(result.message)
    router.push('/login')
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('退出登录失败')
    }
  }
}

const goToProfile = () => {
  router.push('/profile')
}

const goToCollections = () => {
  router.push('/collections')
}
</script>

<template>
  <header>
    <div class="header-content">
      <span id="title"><h3>博客管理系统</h3></span>
      <div class="header-actions">
        <NotificationComponent />
        <div class="user-menu" v-if="userStore.isLoggedIn">
          <el-dropdown @visible-change="userMenuVisible = $event">
            <div class="user-avatar">
              <img v-if="userStore.getAvatarUrl()" :src="userStore.getAvatarUrl()" alt="头像" />
              <div v-else class="avatar-initial">{{ userStore.getUserInitial() }}</div>
              <span class="username">{{ userStore.userInfo.username }}</span>
              <el-icon class="arrow-icon"><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="goToProfile">
                  <el-icon><User /></el-icon>
                  个人中心
                </el-dropdown-item>
                <el-dropdown-item @click="goToCollections">
                  <el-icon><Star /></el-icon>
                  我的收藏
                </el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </div>
  </header>
  <body>
    <div class="list">
        <div></div>
        <RouterLink v-if="userStore.isAdmin()" :to="{ name: 'blogmanagement' }" class="unhold" active-class="hold">博客管理</RouterLink>
        <RouterLink :to="{ name: 'blogview' }" class="unhold" active-class="hold">博客展示</RouterLink>
        <RouterLink :to="{ name: 'collections' }" class="unhold" active-class="hold">我的收藏</RouterLink>
        <RouterLink v-if="userStore.isAdmin()" :to="{ name: 'class' }" class="unhold" active-class="hold">分类管理</RouterLink>
        <RouterLink v-if="userStore.isAdmin()" :to="{ name: 'user' }" class="unhold" active-class="hold">用户管理</RouterLink>
        <RouterLink :to="{ name: 'statistics' }" class="unhold" active-class="hold">统计管理</RouterLink>
        <div></div>
    </div>
    <div class="component">
      <RouterView />
    </div>
  </body>
</template>

<style>
:root {
  --macaron-pink: #ffcfd2;
  --macaron-mint: #b4fae4;
  --macaron-blue: #a3c4f3;
  --macaron-purple: #cdb4db;
  --macaron-yellow: #fde4cf;
  --macaron-grey: #f8f9fa;
  --macaron-white: #ffffff;
  --macaron-dark: #495057;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
}

#app {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: column;
}

header {
  background: linear-gradient(135deg, var(--macaron-purple), var(--macaron-mint),var(--macaron-pink), var(--macaron-purple));
  color: white;
  padding: 20px 30px;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: 0.5px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  flex-shrink: 0;
  width: 100%;
}

.header-content {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 100%;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-menu {
  display: flex;
  align-items: center;
}

.user-avatar {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 8px;
  transition: background-color 0.3s ease;
}

.user-avatar:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.user-avatar img {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 8px;
}

.avatar-initial {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  font-size: 14px;
  margin-right: 8px;
}

.username {
  color: white;
  font-size: 14px;
  margin-right: 4px;
}

.arrow-icon {
  color: white;
  font-size: 12px;
}

body {
  display: flex;
  margin: 0;
  flex: 1;
  font-family: 'Inter', sans-serif;
  overflow: hidden;
}

.list {
  font-weight: 600;
  flex: 0 0 200px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 24px 16px;
  background: var(--macaron-white);
  box-shadow: 2px 0 12px rgba(0, 0, 0, 0.04);
  height: 100%;
  overflow-y: auto;
}

.component {
  flex: 1;
  padding: 32px;
  background: var(--macaron-grey);
  overflow: auto;
  height: 100%;
  min-width: 0;
}

.list a {
  text-decoration: none;
  font-size: 1rem;
  padding: 12px 16px;
  border-radius: 12px;
  transition: all 0.3s ease;
  color: var(--macaron-dark);
  background: var(--macaron-grey);
}

.list a:hover {
  background: var(--macaron-mint);
  transform: translateY(-5px);
}

.list a.hold {
  background: var(--macaron-blue);
  color: var(--macaron-white);
  box-shadow: 0 4px 12px rgba(163, 196, 243, 0.4);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    padding: 0 10px;
  }

  .list {
    flex: 0 0 60px;
    padding: 12px 8px;
  }

  .list a {
    padding: 8px;
    font-size: 0.9rem;
    text-align: center;
  }

  .component {
    padding: 16px;
  }
}

@media (max-width: 480px) {
  header {
    padding: 15px 20px;
    font-size: 1.2rem;
  }

  .list {
    flex: 0 0 50px;
    padding: 8px 4px;
  }

  .list a {
    padding: 6px 4px;
    font-size: 0.8rem;
  }

  .component {
    padding: 12px;
  }
}
</style>
